package cn.icebreak.cartoon.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.icebreak.cartoon.R;
import cn.icebreak.cartoon.global.Route;
import cn.icebreak.cartoon.ui.fragment.DynamicFragment;
import cn.icebreak.cartoon.ui.fragment.MineFragment;
import cn.icebreak.cartoon.ui.fragment.OverviewFragment;
import cn.icebreak.cartoon.ui.read.ItemsFragment;
import cn.icebreak.utils.DensityUtils;

public class DetailActivity extends BaseActivity implements AppBarLayout.OnOffsetChangedListener,ViewPager.OnPageChangeListener,View.OnClickListener {
    private TabLayout tabTl;
    private ViewPager contentVp;
    private TextView describeTv;
    private TextView readTv;


    private AppBarLayout appbar;
    private int mMaxScrollSize;
    private boolean mIsImageHidden;
    private static final int PERCENTAGE_TO_SHOW_IMAGE = 20;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        ButterKnife.bind(this);
        tabTl = findViewById(R.id.tl_detail_tbl);
        contentVp = findViewById(R.id.vp_detail_content);
        describeTv = findViewById(R.id.tv_detail_bottom_describe);
        readTv = findViewById(R.id.tv_detail_bottom_read);
        initViews();
        //AppBarLayout appBarLayout = findViewById(R.id.appbar);
        //appBarLayout.addOnOffsetChangedListener(this);
    }

    private void initViews(){
        ArrayList<Fragment> fragments = new ArrayList<>();
        // 装填
        fragments.add(new OverviewFragment());
        fragments.add(new ItemsFragment());

        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        myPagerAdapter.setFragments(fragments);
        // 给ViewPager设置适配器
        contentVp.setAdapter(myPagerAdapter);
        tabTl.addTab(tabTl.newTab());
        tabTl.addTab(tabTl.newTab());
        // 使用 TabLayout 和 ViewPager 相关联
        tabTl.setupWithViewPager(contentVp);
        // TabLayout指示器添加文本
        tabTl.getTabAt(0).setText("作品介绍");
        tabTl.getTabAt(1).setText("选集");

        contentVp.setOnPageChangeListener(this);
        readTv.setOnClickListener(this);
    }


    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        if (mMaxScrollSize == 0)
            mMaxScrollSize = appBarLayout.getTotalScrollRange();

        int currentScrollPercentage = (Math.abs(i));

        Log.d(TAG, "maxSize = " + mMaxScrollSize + "  currentScrollPercentage=" + currentScrollPercentage + " "+ DensityUtils.px2dp(this,mMaxScrollSize ));
        if (currentScrollPercentage >= PERCENTAGE_TO_SHOW_IMAGE) {
            if (!mIsImageHidden) {
                mIsImageHidden = true;


            }
        }

        if (currentScrollPercentage < PERCENTAGE_TO_SHOW_IMAGE) {
            if (mIsImageHidden) {
                mIsImageHidden = false;

            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
                describeTv.setText("第1话");
                readTv.setText("开始阅读");
                break;
            case 1:
                describeTv.setText("第12话");
                readTv.setText("继续阅读");
                break;
            default:
                describeTv.setText("第1话");
                readTv.setText("开始阅读");
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_detail_bottom_read:
                Route.j2Login(this);
                break;
            default:
                break;
        }
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragmentList;

        public void setFragments(ArrayList<Fragment> fragments) {
            mFragmentList = fragments;
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            Fragment fragment = mFragmentList.get(position);

            return fragment;
        }

        @Override
        public int getCount() {

            return mFragmentList.size();
        }
    }
}
